package com.algorithms.techniques;

import java.util.Arrays;

public class AssemblyLineScheduling
{
	public int[] fastestWay(int[][] stationCost, int[][] crossPathCost,
			int[] startPathCost, int[] endPathCost, int totalStationToTravel)
	{
		int[] minCost = new int[2];
		minCost[0] = startPathCost[0] + stationCost[0][0];
		minCost[1] = startPathCost[1] + stationCost[1][0];
		int[] path0 = new int[totalStationToTravel];
		int[] path1 = new int[totalStationToTravel];
		path0[0] = 0;
		path1[0] = 1;
		int finalMinCost=0;
		int[] finalPath = new int[totalStationToTravel];
		for (int i = 1; i < totalStationToTravel; i++)
		{
			/*
			 * For first line of station
			 */
			if (minCost[0] + stationCost[0][i] < minCost[1]
					+ crossPathCost[1][i - 1] + stationCost[0][i])
			{
				minCost[0] = minCost[0] + stationCost[0][i];
				path0[i] = 0;
			} else
			{
				minCost[0] = minCost[1] + crossPathCost[1][i - 1]
						+ stationCost[0][i];
				path0[i] = 1;
			}
			/*
			 * For second line of station
			 */
			if (minCost[1] + stationCost[1][i] < minCost[0]
					+ crossPathCost[0][i - 1] + stationCost[1][i])
			{
				minCost[1] = minCost[1] + stationCost[1][i];
				path1[i] = 1;
			} else
			{
				minCost[1] = minCost[0] + crossPathCost[0][i - 1]
						+ stationCost[1][i];
				path1[i] = 0;
			}
		}
		minCost[0] = minCost[0] + endPathCost[0];
		minCost[1] = minCost[1] + endPathCost[1];

		if (minCost[1] < minCost[0])
		{
			finalMinCost = minCost[1];
			finalPath = Arrays.copyOf(path1, path1.length);
		} else
		{
			finalMinCost = minCost[0];
			finalPath = Arrays.copyOf(path0, path0.length);
		}
		System.out.println(finalMinCost);
		return finalPath;
	}
}
